<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Java高级学习之-常用类 | kuikui</title><meta name="keywords" content="Java,Java高级"><meta name="author" content="Kuikui Hu"><meta name="copyright" content="Kuikui Hu"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><meta name="description" content="Java常用类一、String类java.lang.String类的使用 1.概述String:字符串，使用一对””引起来表示。1.String声明为final的，不可被继承2.String实现了Serializable接口：表示字符串是支持序列化的。        实现了Comparable接口：表示String可以比较大小3.String内部定义了final char[] value用于存储字">
<meta property="og:type" content="article">
<meta property="og:title" content="Java高级学习之-常用类">
<meta property="og:url" content="https://kuikui1998.gitee.io/2020/06/01/Java%E5%B8%B8%E7%94%A8%E7%B1%BB/index.html">
<meta property="og:site_name" content="kuikui">
<meta property="og:description" content="Java常用类一、String类java.lang.String类的使用 1.概述String:字符串，使用一对””引起来表示。1.String声明为final的，不可被继承2.String实现了Serializable接口：表示字符串是支持序列化的。        实现了Comparable接口：表示String可以比较大小3.String内部定义了final char[] value用于存储字">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/java.jpg">
<meta property="article:published_time" content="2020-06-01T01:15:56.000Z">
<meta property="article:modified_time" content="2020-12-11T08:44:03.362Z">
<meta property="article:author" content="Kuikui Hu">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Java高级">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/java.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://kuikui1998.gitee.io/2020/06/01/Java%E5%B8%B8%E7%94%A8%E7%B1%BB/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  ClickShowText: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  justifiedGallery: {
    js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
    css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
};

var saveToLocal = {
  set: function setWithExpiry(key, value, ttl) {
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-12-11 16:44:03'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = 'false'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
   if (asideStatus === 'hide') {
     document.documentElement.classList.add('hide-aside')
   } else {
     document.documentElement.classList.remove('hide-aside')
   }
}})()</script><meta name="generator" content="Hexo 5.2.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/img/me.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">11</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div></div></div><hr/></div></div><div id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(https://cdn.jsdelivr.net/gh/youthlql/lql_img/blog/top_img.jpg)"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">kuikui</a></span><span id="menus"><span class="close" id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></span></span></nav><div id="post-info"><h1 class="post-title">Java高级学习之-常用类</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-06-01T01:15:56.000Z" title="发表于 2020-06-01 09:15:56">2020-06-01</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2020-12-11T08:44:03.362Z" title="更新于 2020-12-11 16:44:03">2020-12-11</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Java/">Java</a></span></div><div class="meta-secondline"> <span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Java常用类"><a href="#Java常用类" class="headerlink" title="Java常用类"></a>Java常用类</h1><h2 id="一、String类"><a href="#一、String类" class="headerlink" title="一、String类"></a>一、String类</h2><p>java.lang.String类的使用</p>
<h3 id="1-概述"><a href="#1-概述" class="headerlink" title="1.概述"></a>1.概述</h3><p>String:字符串，使用一对””引起来表示。<br>1.String声明为final的，不可被继承<br>2.String实现了Serializable接口：表示字符串是支持序列化的。<br>        实现了Comparable接口：表示String可以比较大小<br>3.String内部定义了final char[] value用于存储字符串数据<br>4.通过字面量的方式（区别于new给一个字符串赋值，此时的字符串值声明在字符串常量池中)。<br>5.字符串常量池中是不会存储相同内容(使用String类的equals()比较，返回true)的字符串的。</p>
<h3 id="2-String的不可变性"><a href="#2-String的不可变性" class="headerlink" title="2.String的不可变性"></a>2.String的不可变性</h3><p>2.1 说明<br>1.当对字符串重新赋值时，需要重写指定内存区域赋值，不能使用原有的value进行赋值。<br>2.当对现的字符串进行连接操作时，也需要重新指定内存区域赋值，不能使用原有的value进行赋值。<br>3.当调用String的replace()方法修改指定字符或字符串时，也需要重新指定内存区域赋值，不能使用原有的value进行赋值。<br>2.2 代码举例</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">String s1 = <span class="string">&quot;abc&quot;</span>;<span class="comment">//字面量的定义方式</span></span><br><span class="line">String s2 = <span class="string">&quot;abc&quot;</span>;</span><br><span class="line">s1 = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line"></span><br><span class="line">System.out.println(s1 == s2);<span class="comment">//比较s1和s2的地址值</span></span><br><span class="line"></span><br><span class="line">System.out.println(s1);<span class="comment">//hello</span></span><br><span class="line">System.out.println(s2);<span class="comment">//abc</span></span><br><span class="line"></span><br><span class="line">System.out.println(<span class="string">&quot;*****************&quot;</span>);</span><br><span class="line"></span><br><span class="line">String s3 = <span class="string">&quot;abc&quot;</span>;</span><br><span class="line">s3 += <span class="string">&quot;def&quot;</span>;</span><br><span class="line">System.out.println(s3);<span class="comment">//abcdef</span></span><br><span class="line">System.out.println(s2);</span><br><span class="line"></span><br><span class="line">System.out.println(<span class="string">&quot;*****************&quot;</span>);</span><br><span class="line"></span><br><span class="line">String s4 = <span class="string">&quot;abc&quot;</span>;</span><br><span class="line">String s5 = s4.replace(<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;m&#x27;</span>);</span><br><span class="line">System.out.println(s4);<span class="comment">//abc</span></span><br><span class="line">System.out.println(s5);<span class="comment">//mbc</span></span><br></pre></td></tr></table></figure>

<p>2.3 图示</p>
<p><img src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/Java%E5%B8%B8%E7%94%A8%E7%B1%BB/String%E4%B8%8D%E5%8F%AF%E5%8F%98%E6%80%A7.png" alt="image-20200530100056065"></p>
<h3 id="3-String实例化的不同方式"><a href="#3-String实例化的不同方式" class="headerlink" title="3.String实例化的不同方式"></a>3.String实例化的不同方式</h3><p>3.1 方式说明<br>方式一：通过字面量定义的方式<br>方式二：通过new + 构造器的方式<br>3.2 代码举例</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//通过字面量定义的方式：此时的s1和s2的数据javaEE声明在方法区中的字符串常量池中。</span></span><br><span class="line">String s1 = <span class="string">&quot;javaEE&quot;</span>;</span><br><span class="line">String s2 = <span class="string">&quot;javaEE&quot;</span>;</span><br><span class="line"><span class="comment">//通过new + 构造器的方式:此时的s3和s4保存的地址值，是数据在堆空间中开辟空间以后对应的地址值。</span></span><br><span class="line">String s3 = <span class="keyword">new</span> String(<span class="string">&quot;javaEE&quot;</span>);</span><br><span class="line">String s4 = <span class="keyword">new</span> String(<span class="string">&quot;javaEE&quot;</span>);</span><br><span class="line"></span><br><span class="line">System.out.println(s1 == s2);<span class="comment">//true</span></span><br><span class="line">System.out.println(s1 == s3);<span class="comment">//false</span></span><br><span class="line">System.out.println(s1 == s4);<span class="comment">//false</span></span><br><span class="line">System.out.println(s3 == s4);<span class="comment">//false</span></span><br></pre></td></tr></table></figure>

<p>3.3 面试题<br>String s = new String(“abc”);方式创建对象，在内存中创建了几个对象？<br>两个:一个是堆空间中new结构，另一个是char[]对应的常量池中的数据：”abc”<br>3.4 图示</p>
<p><img src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/Java%E5%B8%B8%E7%94%A8%E7%B1%BB/String%E5%AE%9E%E4%BE%8B%E5%8C%96.png" alt="image-20200530100202315"></p>
<h3 id="4-字符串拼接方式赋值的对比"><a href="#4-字符串拼接方式赋值的对比" class="headerlink" title="4.字符串拼接方式赋值的对比"></a>4.字符串拼接方式赋值的对比</h3><p>4.1 说明<br>1.常量与常量的拼接结果在常量池。且常量池中不会存在相同内容的常量。<br>2.只要其中一个是变量，结果就在堆中。<br>3.如果拼接的结果调用intern()方法，返回值就在常量池中<br>4.2 代码举例</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">String s1 = <span class="string">&quot;javaEE&quot;</span>;</span><br><span class="line">String s2 = <span class="string">&quot;hadoop&quot;</span>;</span><br><span class="line"></span><br><span class="line">String s3 = <span class="string">&quot;javaEEhadoop&quot;</span>;</span><br><span class="line">String s4 = <span class="string">&quot;javaEE&quot;</span> + <span class="string">&quot;hadoop&quot;</span>;</span><br><span class="line">String s5 = s1 + <span class="string">&quot;hadoop&quot;</span>;</span><br><span class="line">String s6 = <span class="string">&quot;javaEE&quot;</span> + s2;</span><br><span class="line">String s7 = s1 + s2;</span><br><span class="line"></span><br><span class="line">System.out.println(s3 == s4);<span class="comment">//true</span></span><br><span class="line">System.out.println(s3 == s5);<span class="comment">//false</span></span><br><span class="line">System.out.println(s3 == s6);<span class="comment">//false</span></span><br><span class="line">System.out.println(s3 == s7);<span class="comment">//false</span></span><br><span class="line">System.out.println(s5 == s6);<span class="comment">//false</span></span><br><span class="line">System.out.println(s5 == s7);<span class="comment">//false</span></span><br><span class="line">System.out.println(s6 == s7);<span class="comment">//false</span></span><br><span class="line"></span><br><span class="line">String s8 = s6.intern();<span class="comment">//返回值得到的s8使用的常量值中已经存在的“javaEEhadoop”</span></span><br><span class="line">System.out.println(s3 == s8);<span class="comment">//true</span></span><br><span class="line">****************************</span><br><span class="line">String s1 = <span class="string">&quot;javaEEhadoop&quot;</span>;</span><br><span class="line">String s2 = <span class="string">&quot;javaEE&quot;</span>;</span><br><span class="line">String s3 = s2 + <span class="string">&quot;hadoop&quot;</span>;</span><br><span class="line">System.out.println(s1 == s3);<span class="comment">//false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">final</span> String s4 = <span class="string">&quot;javaEE&quot;</span>;<span class="comment">//s4:常量</span></span><br><span class="line">String s5 = s4 + <span class="string">&quot;hadoop&quot;</span>;</span><br><span class="line">System.out.println(s1 == s5);<span class="comment">//true</span></span><br></pre></td></tr></table></figure>

<p>###5.常用方法：</p>
<p>int length()：返回字符串的长度： return value.length<br>char charAt(int index)： 返回某索引处的字符return value[index]<br>boolean isEmpty()：判断是否是空字符串：return value.length == 0<br>String toLowerCase()：使用默认语言环境，将 String 中的所字符转换为小写<br>String toUpperCase()：使用默认语言环境，将 String 中的所字符转换为大写<br>String trim()：返回字符串的副本，忽略前导空白和尾部空白<br>boolean equals(Object obj)：比较字符串的内容是否相同<br>boolean equalsIgnoreCase(String anotherString)：与equals方法类似，忽略大小写<br>String concat(String str)：将指定字符串连接到此字符串的结尾。 等价于用“+”<br>int compareTo(String anotherString)：比较两个字符串的大小<br>String substring(int beginIndex)：返回一个新的字符串，它是此字符串的从beginIndex开始截取到最后的一个子字符串。<br>String substring(int beginIndex, int endIndex) ：返回一个新字符串，它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。</p>
<p>boolean endsWith(String suffix)：测试此字符串是否以指定的后缀结束<br>boolean startsWith(String prefix)：测试此字符串是否以指定的前缀开始<br>boolean startsWith(String prefix, int toffset)：测试此字符串从指定索引开始的子字符串是否以指定前缀开始</p>
<p>boolean contains(CharSequence s)：当且仅当此字符串包含指定的 char 值序列时，返回 true<br>int indexOf(String str)：返回指定子字符串在此字符串中第一次出现处的索引<br>int indexOf(String str, int fromIndex)：返回指定子字符串在此字符串中第一次出现处的索引，从指定的索引开始<br>int lastIndexOf(String str)：返回指定子字符串在此字符串中最右边出现处的索引<br>int lastIndexOf(String str, int fromIndex)：返回指定子字符串在此字符串中最后一次出现处的索引，从指定的索引开始反向搜索</p>
<p>注：indexOf和lastIndexOf方法如果未找到都是返回-1</p>
<p>替换：<br>String replace(char oldChar, char newChar)：返回一个新的字符串，它是通过用 newChar 替换此字符串中出现的所 oldChar 得到的。<br>String replace(CharSequence target, CharSequence replacement)：使用指定的字面值替换序列替换此字符串所匹配字面值目标序列的子字符串。<br>String replaceAll(String regex, String replacement)：使用给定的 replacement 替换此字符串所匹配给定的正则表达式的子字符串。<br>String replaceFirst(String regex, String replacement)：使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。<br>匹配:<br>boolean matches(String regex)：告知此字符串是否匹配给定的正则表达式。<br>切片：<br>String[] split(String regex)：根据给定正则表达式的匹配拆分此字符串。<br>String[] split(String regex, int limit)：根据匹配给定的正则表达式来拆分此字符串，最多不超过limit个，如果超过了，剩下的全部都放到最后一个元素中。</p>
<h3 id="6-String与其它结构的转换"><a href="#6-String与其它结构的转换" class="headerlink" title="6.String与其它结构的转换"></a>6.String与其它结构的转换</h3><h4 id="6-1-与基本数据类型、包装类之间的转换"><a href="#6-1-与基本数据类型、包装类之间的转换" class="headerlink" title="6.1 与基本数据类型、包装类之间的转换"></a>6.1 与基本数据类型、包装类之间的转换</h4><p>  String –&gt; 基本数据类型、包装类：调用包装类的静态方法：parseXxx(str)<br>  基本数据类型、包装类 –&gt; String:调用String重载的valueOf(xxx)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span></span>&#123;</span><br><span class="line">        String str1 = <span class="string">&quot;123&quot;</span>;</span><br><span class="line"><span class="comment">//        int num = (int)str1;//错误的</span></span><br><span class="line">        <span class="keyword">int</span> num = Integer.parseInt(str1);</span><br><span class="line"></span><br><span class="line">        String str2 = String.valueOf(num);<span class="comment">//&quot;123&quot;</span></span><br><span class="line">        String str3 = num + <span class="string">&quot;&quot;</span>;</span><br><span class="line"></span><br><span class="line">        System.out.println(str1 == str3);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h4 id="6-2-与字符数组之间的转换"><a href="#6-2-与字符数组之间的转换" class="headerlink" title="6.2 与字符数组之间的转换"></a>6.2 与字符数组之间的转换</h4><p>String –&gt; char[]:调用String的toCharArray()<br>char[] –&gt; String:调用String的构造器</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span></span>&#123;</span><br><span class="line">    String str1 = <span class="string">&quot;abc123&quot;</span>;  <span class="comment">//题目： a21cb3</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">char</span>[] charArray = str1.toCharArray();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; charArray.length; i++) &#123;</span><br><span class="line">        System.out.println(charArray[i]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">char</span>[] arr = <span class="keyword">new</span> <span class="keyword">char</span>[]&#123;<span class="string">&#x27;h&#x27;</span>,<span class="string">&#x27;e&#x27;</span>,<span class="string">&#x27;l&#x27;</span>,<span class="string">&#x27;l&#x27;</span>,<span class="string">&#x27;o&#x27;</span>&#125;;</span><br><span class="line">    String str2 = <span class="keyword">new</span> String(arr);</span><br><span class="line">    System.out.println(str2);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="6-3-与字节数组之间的转换"><a href="#6-3-与字节数组之间的转换" class="headerlink" title="6.3 与字节数组之间的转换"></a>6.3 与字节数组之间的转换</h4><p>编码：String –&gt; byte[]:调用String的getBytes()<br>解码：byte[] –&gt; String:调用String的构造器</p>
<p>编码：字符串 –&gt;字节  (看得懂 —&gt;看不懂的二进制数据)<br>解码：编码的逆过程，字节 –&gt; 字符串 （看不懂的二进制数据 —&gt; 看得懂</p>
<p>说明：解码时，要求解码使用的字符集必须与编码时使用的字符集一致，否则会出现乱码。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test3</span><span class="params">()</span> <span class="keyword">throws</span> UnsupportedEncodingException </span>&#123;</span><br><span class="line">    String str1 = <span class="string">&quot;abc123中国&quot;</span>;</span><br><span class="line">    <span class="keyword">byte</span>[] bytes = str1.getBytes();<span class="comment">//使用默认的字符集，进行编码。</span></span><br><span class="line">    System.out.println(Arrays.toString(bytes));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">byte</span>[] gbks = str1.getBytes(<span class="string">&quot;gbk&quot;</span>);<span class="comment">//使用gbk字符集进行编码。</span></span><br><span class="line">    System.out.println(Arrays.toString(gbks));</span><br><span class="line"></span><br><span class="line">    System.out.println(<span class="string">&quot;******************&quot;</span>);</span><br><span class="line"></span><br><span class="line">    String str2 = <span class="keyword">new</span> String(bytes);<span class="comment">//使用默认的字符集，进行解码。</span></span><br><span class="line">    System.out.println(str2);</span><br><span class="line"></span><br><span class="line">    String str3 = <span class="keyword">new</span> String(gbks);</span><br><span class="line">    System.out.println(str3);<span class="comment">//出现乱码。原因：编码集和解码集不一致！</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    String str4 = <span class="keyword">new</span> String(gbks, <span class="string">&quot;gbk&quot;</span>);</span><br><span class="line">    System.out.println(str4);<span class="comment">//没出现乱码。原因：编码集和解码集一致！</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="6-4-与StringBuffer、StringBuilder之间的转换"><a href="#6-4-与StringBuffer、StringBuilder之间的转换" class="headerlink" title="6.4 与StringBuffer、StringBuilder之间的转换"></a>6.4 与StringBuffer、StringBuilder之间的转换</h4><p>String –&gt;StringBuffer、StringBuilder:调用StringBuffer、StringBuilder构造器<br>StringBuffer、StringBuilder –&gt;String:①调用String构造器；②StringBuffer、StringBuilder的toString()</p>
<h3 id="7-JVM中字符串常量池存放位置说明："><a href="#7-JVM中字符串常量池存放位置说明：" class="headerlink" title="7.JVM中字符串常量池存放位置说明："></a>7.JVM中字符串常量池存放位置说明：</h3><p>jdk 1.6 (jdk 6.0 ,java 6.0):字符串常量池存储在方法区（永久区）<br>jdk 1.7:字符串常量池存储在堆空间<br>jdk 1.8:字符串常量池存储在方法区（元空间）</p>
<h3 id="8-常见算法题目的考查："><a href="#8-常见算法题目的考查：" class="headerlink" title="8.常见算法题目的考查："></a>8.常见算法题目的考查：</h3><p>1）模拟一个trim方法，去除字符串两端的空格。</p>
<p>2）将一个字符串进行反转。将字符串中指定部分进行反转。比如“abcdefg”反转为”abfedcg”</p>
<p>3）获取一个字符串在另一个字符串中出现的次数。<br>      比如：获取“ ab”在 “abkkcadkabkebfkabkskab” 中出现的次数</p>
<p>4）获取两个字符串中最大相同子串。比如：<br>   str1 = “abcwerthelloyuiodef“;str2 = “cvhellobnm”<br>   提示：将短的那个串进行长度依次递减的子串与较长的串比较。</p>
<p>5）对字符串中字符进行自然顺序排序。<br>提示：<br>1字符串变成字符数组。<br>2对数组排序，择，冒泡，Arrays.sort();<br>3将排序后的数组变成字符串。</p>
<h2 id="二、StringBuffer、StringBuilder"><a href="#二、StringBuffer、StringBuilder" class="headerlink" title="二、StringBuffer、StringBuilder"></a>二、StringBuffer、StringBuilder</h2><h3 id="1-String、StringBuffer、StringBuilder三者的对比"><a href="#1-String、StringBuffer、StringBuilder三者的对比" class="headerlink" title="1.String、StringBuffer、StringBuilder三者的对比"></a>1.String、StringBuffer、StringBuilder三者的对比</h3><p>String:不可变的字符序列；底层使用char[]存储<br>StringBuffer:可变的字符序列；线程安全的，效率低；底层使用char[]存储<br>StringBuilder:可变的字符序列；jdk5.0新增的，线程不安全的，效率高；底层使用char[]存储</p>
<h3 id="2-StringBuffer与StringBuilder的内存解析"><a href="#2-StringBuffer与StringBuilder的内存解析" class="headerlink" title="2.StringBuffer与StringBuilder的内存解析"></a>2.StringBuffer与StringBuilder的内存解析</h3><p>以StringBuffer为例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">String str = <span class="keyword">new</span> String();<span class="comment">//char[] value = new char[0];</span></span><br><span class="line">String str1 = <span class="keyword">new</span> String(<span class="string">&quot;abc&quot;</span>);<span class="comment">//char[] value = new char[]&#123;&#x27;a&#x27;,&#x27;b&#x27;,&#x27;c&#x27;&#125;;</span></span><br><span class="line"></span><br><span class="line">StringBuffer sb1 = <span class="keyword">new</span> StringBuffer();<span class="comment">//char[] value = new char[16];底层创建了一个长度是16的数组。</span></span><br><span class="line">System.out.println(sb1.length());<span class="comment">//</span></span><br><span class="line">sb1.append(<span class="string">&#x27;a&#x27;</span>);<span class="comment">//value[0] = &#x27;a&#x27;;</span></span><br><span class="line">sb1.append(<span class="string">&#x27;b&#x27;</span>);<span class="comment">//value[1] = &#x27;b&#x27;;</span></span><br><span class="line"></span><br><span class="line">StringBuffer sb2 = <span class="keyword">new</span> StringBuffer(<span class="string">&quot;abc&quot;</span>);<span class="comment">//char[] value = new char[&quot;abc&quot;.length() + 16];</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//问题1. System.out.println(sb2.length());//3</span></span><br><span class="line"><span class="comment">//问题2. 扩容问题:如果要添加的数据底层数组盛不下了，那就需要扩容底层的数组。</span></span><br><span class="line">         默认情况下，扩容为原来容量的<span class="number">2</span>倍 + <span class="number">2</span>，同时将原数组中的元素复制到新的数组中。</span><br><span class="line"></span><br><span class="line">        指导意义：开发中建议大家使用：StringBuffer(<span class="keyword">int</span> capacity) 或 StringBuilder(<span class="keyword">int</span> capacity)</span><br></pre></td></tr></table></figure>

<h3 id="3-对比String、StringBuffer、StringBuilder三者的执行效率"><a href="#3-对比String、StringBuffer、StringBuilder三者的执行效率" class="headerlink" title="3.对比String、StringBuffer、StringBuilder三者的执行效率"></a>3.对比String、StringBuffer、StringBuilder三者的执行效率</h3><p>从高到低排列：StringBuilder &gt; StringBuffer &gt; String</p>
<h3 id="4-StringBuffer、StringBuilder中的常用方法"><a href="#4-StringBuffer、StringBuilder中的常用方法" class="headerlink" title="4.StringBuffer、StringBuilder中的常用方法"></a>4.StringBuffer、StringBuilder中的常用方法</h3><p>增：append(xxx)<br>删：delete(int start,int end)<br>改：setCharAt(int n ,char ch) / replace(int start, int end, String str)<br>查：charAt(int n )<br>插：insert(int offset, xxx)<br>长度：length();<br>*遍历：for() + charAt() / toString()</p>
<h2 id="三、JDK-8之前日期时间API"><a href="#三、JDK-8之前日期时间API" class="headerlink" title="三、JDK 8之前日期时间API"></a>三、JDK 8之前日期时间API</h2><h3 id="1-获取系统当前时间：System类中的currentTimeMillis"><a href="#1-获取系统当前时间：System类中的currentTimeMillis" class="headerlink" title="1.获取系统当前时间：System类中的currentTimeMillis()"></a>1.获取系统当前时间：System类中的currentTimeMillis()</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">long</span> time = System.currentTimeMillis();</span><br><span class="line"><span class="comment">//返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。</span></span><br><span class="line"><span class="comment">//称为时间戳</span></span><br><span class="line">System.out.println(time);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="2-java-util-Date类与java-sql-Date类"><a href="#2-java-util-Date类与java-sql-Date类" class="headerlink" title="2.java.util.Date类与java.sql.Date类"></a>2.java.util.Date类与java.sql.Date类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    java.util.Date类</span></span><br><span class="line"><span class="comment">           |---java.sql.Date类</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    1.两个构造器的使用</span></span><br><span class="line"><span class="comment">        &gt;构造器一：Date()：创建一个对应当前时间的Date对象</span></span><br><span class="line"><span class="comment">        &gt;构造器二：创建指定毫秒数的Date对象</span></span><br><span class="line"><span class="comment">    2.两个方法的使用</span></span><br><span class="line"><span class="comment">        &gt;toString():显示当前的年、月、日、时、分、秒</span></span><br><span class="line"><span class="comment">        &gt;getTime():获取当前Date对象对应的毫秒数。（时间戳）</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    3. java.sql.Date对应着数据库中的日期类型的变量</span></span><br><span class="line"><span class="comment">        &gt;如何实例化</span></span><br><span class="line"><span class="comment">        &gt;如何将java.util.Date对象转换为java.sql.Date对象</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">//构造器一：Date()：创建一个对应当前时间的Date对象</span></span><br><span class="line">        Date date1 = <span class="keyword">new</span> Date();</span><br><span class="line">        System.out.println(date1.toString());<span class="comment">//Sat Feb 16 16:35:31 GMT+08:00 2019</span></span><br><span class="line"></span><br><span class="line">        System.out.println(date1.getTime());<span class="comment">//1550306204104</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//构造器二：创建指定毫秒数的Date对象</span></span><br><span class="line">        Date date2 = <span class="keyword">new</span> Date(<span class="number">155030620410L</span>);</span><br><span class="line">        System.out.println(date2.toString());</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建java.sql.Date对象</span></span><br><span class="line">        java.sql.Date date3 = <span class="keyword">new</span> java.sql.Date(<span class="number">35235325345L</span>);</span><br><span class="line">        System.out.println(date3);<span class="comment">//1971-02-13</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//如何将java.util.Date对象转换为java.sql.Date对象</span></span><br><span class="line">        <span class="comment">//情况一：</span></span><br><span class="line"><span class="comment">//        Date date4 = new java.sql.Date(2343243242323L);</span></span><br><span class="line"><span class="comment">//        java.sql.Date date5 = (java.sql.Date) date4;</span></span><br><span class="line">        <span class="comment">//情况二：</span></span><br><span class="line">        Date date6 = <span class="keyword">new</span> Date();</span><br><span class="line">        java.sql.Date date7 = <span class="keyword">new</span> java.sql.Date(date6.getTime());</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-java-text-SimpleDataFormat类"><a href="#3-java-text-SimpleDataFormat类" class="headerlink" title="3.java.text.SimpleDataFormat类"></a>3.java.text.SimpleDataFormat类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">SimpleDateFormat对日期Date类的格式化和解析</span><br><span class="line"><span class="number">1.</span>两个操作：</span><br><span class="line"><span class="number">1.1</span> 格式化：日期 ---&gt;字符串</span><br><span class="line"><span class="number">1.2</span> 解析：格式化的逆过程，字符串 ---&gt; 日期</span><br><span class="line"></span><br><span class="line"><span class="number">2.</span>SimpleDateFormat的实例化:<span class="keyword">new</span> + 构造器</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//*************照指定的方式格式化和解析：调用带参的构造器*****************</span></span><br><span class="line"><span class="comment">//        SimpleDateFormat sdf1 = new SimpleDateFormat(&quot;yyyyy.MMMMM.dd GGG hh:mm aaa&quot;);</span></span><br><span class="line">        SimpleDateFormat sdf1 = <span class="keyword">new</span> SimpleDateFormat(<span class="string">&quot;yyyy-MM-dd hh:mm:ss&quot;</span>);</span><br><span class="line">        <span class="comment">//格式化</span></span><br><span class="line">        String format1 = sdf1.format(date);</span><br><span class="line">        System.out.println(format1);<span class="comment">//2019-02-18 11:48:27</span></span><br><span class="line">        <span class="comment">//解析:要求字符串必须是符合SimpleDateFormat识别的格式(通过构造器参数体现),</span></span><br><span class="line">        <span class="comment">//否则，抛异常</span></span><br><span class="line">        Date date2 = sdf1.parse(<span class="string">&quot;2020-02-18 11:48:27&quot;</span>);</span><br><span class="line">        System.out.println(date2);</span><br></pre></td></tr></table></figure>

<p><strong>小练习：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    练习一：字符串&quot;2020-09-08&quot;转换为java.sql.Date</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    练习二：&quot;天打渔两天晒网&quot;   1990-01-01  xxxx-xx-xx 打渔？晒网？</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    举例：2020-09-08 ？ 总天数</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    总天数 % 5 == 1,2,3 : 打渔</span></span><br><span class="line"><span class="comment">    总天数 % 5 == 4,0 : 晒网</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    总天数的计算？</span></span><br><span class="line"><span class="comment">    方式一：( date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24) + 1</span></span><br><span class="line"><span class="comment">    方式二：1990-01-01  --&gt; 2019-12-31  +  2020-01-01 --&gt;2020-09-08</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testExer</span><span class="params">()</span> <span class="keyword">throws</span> ParseException </span>&#123;</span><br><span class="line">        String birth = <span class="string">&quot;2020-09-08&quot;</span>;</span><br><span class="line"></span><br><span class="line">        SimpleDateFormat sdf1 = <span class="keyword">new</span> SimpleDateFormat(<span class="string">&quot;yyyy-MM-dd&quot;</span>);</span><br><span class="line">        Date date = sdf1.parse(birth);</span><br><span class="line"><span class="comment">//        System.out.println(date);</span></span><br><span class="line"></span><br><span class="line">        java.sql.Date birthDate = <span class="keyword">new</span> java.sql.Date(date.getTime());</span><br><span class="line">        System.out.println(birthDate);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="4-Calendar类：日历类、抽象类"><a href="#4-Calendar类：日历类、抽象类" class="headerlink" title="4.Calendar类：日历类、抽象类"></a>4.Calendar类：日历类、抽象类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1.实例化</span></span><br><span class="line">        <span class="comment">//方式一：创建其子类（GregorianCalendar的对象</span></span><br><span class="line">        <span class="comment">//方式二：调用其静态方法getInstance()</span></span><br><span class="line">        Calendar calendar = Calendar.getInstance();</span><br><span class="line"><span class="comment">//        System.out.println(calendar.getClass());</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//2.常用方法</span></span><br><span class="line">        <span class="comment">//get()</span></span><br><span class="line">        <span class="keyword">int</span> days = calendar.get(Calendar.DAY_OF_MONTH);</span><br><span class="line">        System.out.println(days);</span><br><span class="line">        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//set()</span></span><br><span class="line">        <span class="comment">//calendar可变性</span></span><br><span class="line">         calendar.set(Calendar.DAY_OF_MONTH,<span class="number">22</span>);</span><br><span class="line">        days = calendar.get(Calendar.DAY_OF_MONTH);</span><br><span class="line">        System.out.println(days);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//add()</span></span><br><span class="line">        calendar.add(Calendar.DAY_OF_MONTH,-<span class="number">3</span>);</span><br><span class="line">        days = calendar.get(Calendar.DAY_OF_MONTH);</span><br><span class="line">        System.out.println(days);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//getTime():日历类---&gt; Date</span></span><br><span class="line">        Date date = calendar.getTime();</span><br><span class="line">        System.out.println(date);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//setTime():Date ---&gt; 日历类</span></span><br><span class="line">        Date date1 = <span class="keyword">new</span> Date();</span><br><span class="line">        calendar.setTime(date1);</span><br><span class="line">        days = calendar.get(Calendar.DAY_OF_MONTH);</span><br><span class="line">        System.out.println(days);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="四、JDK8中新日期时间API"><a href="#四、JDK8中新日期时间API" class="headerlink" title="四、JDK8中新日期时间API"></a>四、JDK8中新日期时间API</h2><h3 id="1-日期时间API的迭代："><a href="#1-日期时间API的迭代：" class="headerlink" title="1.日期时间API的迭代："></a>1.日期时间API的迭代：</h3><p>第一代：jdk 1.0 Date类<br>第二代：jdk 1.1 Calendar类，一定程度上替换Date类<br>第三代：jdk 1.8 提出了新的一套API</p>
<h3 id="2-前两代存在的问题举例："><a href="#2-前两代存在的问题举例：" class="headerlink" title="2.前两代存在的问题举例："></a>2.前两代存在的问题举例：</h3><p>可变性：像日期和时间这样的类应该是不可变的。<br>偏移性：Date中的年份是从1900开始的，而月份都从0开始。<br>格式化：格式化只对Date用，Calendar则不行。<br>此外，它们也不是线程安全的；不能处理闰秒等。</p>
<h3 id="3-java-8-中新的日期时间API涉及到的包"><a href="#3-java-8-中新的日期时间API涉及到的包" class="headerlink" title="3.java 8 中新的日期时间API涉及到的包"></a>3.java 8 中新的日期时间API涉及到的包</h3><p><img src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/Java%E5%B8%B8%E7%94%A8%E7%B1%BB/JDK8%E6%96%B0%E6%97%B6%E9%97%B4API.png" alt="image-20200530101150825"></p>
<h3 id="4-本地日期、本地时间、本地日期时间的使用：LocalDate-LocalTime-LocalDateTime"><a href="#4-本地日期、本地时间、本地日期时间的使用：LocalDate-LocalTime-LocalDateTime" class="headerlink" title="4.==本地日期、本地时间、本地日期时间的使用：LocalDate / LocalTime / LocalDateTime=="></a>4.==本地日期、本地时间、本地日期时间的使用：LocalDate / LocalTime / LocalDateTime==</h3><p>4.1 说明：<br>① 分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的本地日期或时间，并不包含当前的时间信息，也不包含与时区相关的信息。<br>② LocalDateTime相较于LocalDate、LocalTime，使用频率要高<br>③ 类似于Calendar</p>
<p>4.2 常用方法：</p>
<p><img src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/Java%E5%B8%B8%E7%94%A8%E7%B1%BB/%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95.png" alt="image-20200530101242228"></p>
<h3 id="5-时间点：Instant"><a href="#5-时间点：Instant" class="headerlink" title="==5.时间点：Instant=="></a>==5.时间点：Instant==</h3><p>5.1 说明：<br>① 时间线上的一个瞬时点。 概念上讲，它只是简单的表示自1970年1月1日0时0分0秒（UTC开始的秒数。）<br>② 类似于 java.util.Date类</p>
<p>5.2 常用方法：</p>
<p><img src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/Java%E5%B8%B8%E7%94%A8%E7%B1%BB/Instant%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95.png" alt="image-20200530101323554"></p>
<h3 id="6-日期时间格式化类：DateTimeFormatter"><a href="#6-日期时间格式化类：DateTimeFormatter" class="headerlink" title="6.日期时间格式化类：DateTimeFormatter"></a>6.日期时间格式化类：DateTimeFormatter</h3><p>6.1 说明：<br>① 格式化或解析日期、时间<br>② 类似于SimpleDateFormat<br>6.2 常用方法：<br>① 实例化方式：<br>预定义的标准格式。如：ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME<br>本地化相关的格式。如：ofLocalizedDateTime(FormatStyle.LONG)<br>自定义的格式。如：ofPattern(“yyyy-MM-dd hh:mm:ss”)</p>
<p>② 常用方法：</p>
<p><img src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/Java%E5%B8%B8%E7%94%A8%E7%B1%BB/datetimeFormat%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95.png" alt="image-20200530101402916"></p>
<p>特别的：==自定义的格式。如：ofPattern(“yyyy-MM-dd hh:mm:ss”)==</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  重点：自定义的格式。如：ofPattern(“yyyy-MM-dd hh:mm:ss”)</span></span><br><span class="line">DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern(<span class="string">&quot;yyyy-MM-dd hh:mm:ss&quot;</span>);</span><br><span class="line"><span class="comment">//格式化</span></span><br><span class="line">String str4 = formatter3.format(LocalDateTime.now());</span><br><span class="line">System.out.println(str4);<span class="comment">//2019-02-18 03:52:09</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//解析</span></span><br><span class="line">TemporalAccessor accessor = formatter3.parse(<span class="string">&quot;2019-02-18 03:52:09&quot;</span>);</span><br><span class="line">System.out.println(accessor);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="7-其它API的使用-（不讲）"><a href="#7-其它API的使用-（不讲）" class="headerlink" title="7.其它API的使用 （不讲）"></a>7.其它API的使用 （不讲）</h3><h4 id="7-1-带时区的日期时间：ZonedDateTime-ZoneId"><a href="#7-1-带时区的日期时间：ZonedDateTime-ZoneId" class="headerlink" title="7.1 带时区的日期时间：ZonedDateTime / ZoneId"></a>7.1 带时区的日期时间：ZonedDateTime / ZoneId</h4><p>举例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">举例：</span><br><span class="line"><span class="comment">// ZoneId:类中包含了所的时区信息</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="comment">//getAvailableZoneIds():获取所的ZoneId</span></span><br><span class="line">		Set&lt;String&gt; zoneIds = ZoneId.getAvailableZoneIds();</span><br><span class="line">		<span class="keyword">for</span>(String s : zoneIds)&#123;</span><br><span class="line">			System.out.println(s);</span><br><span class="line">		&#125;</span><br><span class="line">		System.out.println();</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//获取“Asia/Tokyo”时区对应的时间</span></span><br><span class="line">		LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of(<span class="string">&quot;Asia/Tokyo&quot;</span>));</span><br><span class="line">		System.out.println(localDateTime);</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"><span class="comment">//ZonedDateTime:带时区的日期时间</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="comment">//now():获取本时区的ZonedDateTime对象</span></span><br><span class="line">		ZonedDateTime zonedDateTime = ZonedDateTime.now();</span><br><span class="line">		System.out.println(zonedDateTime);</span><br><span class="line">		<span class="comment">//now(ZoneId id):获取指定时区的ZonedDateTime对象</span></span><br><span class="line">		ZonedDateTime zonedDateTime1 = ZonedDateTime.now(ZoneId.of(<span class="string">&quot;Asia/Tokyo&quot;</span>));</span><br><span class="line">		System.out.println(zonedDateTime1);</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h4 id="7-2-时间间隔：Duration–用于计算两个“时间”间隔，以秒和纳秒为基准"><a href="#7-2-时间间隔：Duration–用于计算两个“时间”间隔，以秒和纳秒为基准" class="headerlink" title="7.2 时间间隔：Duration–用于计算两个“时间”间隔，以秒和纳秒为基准"></a>7.2 时间间隔：Duration–用于计算两个“时间”间隔，以秒和纳秒为基准</h4><p><img src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/Java%E5%B8%B8%E7%94%A8%E7%B1%BB/%E6%97%B6%E9%97%B4%E9%97%B4%E9%9A%94%E6%96%B9%E6%B3%95.png" alt="image-20200530101523841"></p>
<p>举例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test3</span><span class="params">()</span></span>&#123;</span><br><span class="line">		LocalTime localTime = LocalTime.now();</span><br><span class="line">		LocalTime localTime1 = LocalTime.of(<span class="number">15</span>, <span class="number">23</span>, <span class="number">32</span>);</span><br><span class="line">		<span class="comment">//between():静态方法，返回Duration对象，表示两个时间的间隔</span></span><br><span class="line">		Duration duration = Duration.between(localTime1, localTime);</span><br><span class="line">		System.out.println(duration);</span><br><span class="line">		</span><br><span class="line">		System.out.println(duration.getSeconds());</span><br><span class="line">		System.out.println(duration.getNano());</span><br><span class="line">		</span><br><span class="line">		LocalDateTime localDateTime = LocalDateTime.of(<span class="number">2016</span>, <span class="number">6</span>, <span class="number">12</span>, <span class="number">15</span>, <span class="number">23</span>, <span class="number">32</span>);</span><br><span class="line">		LocalDateTime localDateTime1 = LocalDateTime.of(<span class="number">2017</span>, <span class="number">6</span>, <span class="number">12</span>, <span class="number">15</span>, <span class="number">23</span>, <span class="number">32</span>);</span><br><span class="line">		</span><br><span class="line">		Duration duration1 = Duration.between(localDateTime1, localDateTime);</span><br><span class="line">		System.out.println(duration1.toDays());</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h4 id="7-3-日期间隔：Period-–用于计算两个“日期”间隔，以年、月、日衡量"><a href="#7-3-日期间隔：Period-–用于计算两个“日期”间隔，以年、月、日衡量" class="headerlink" title="7.3 日期间隔：Period –用于计算两个“日期”间隔，以年、月、日衡量"></a>7.3 日期间隔：Period –用于计算两个“日期”间隔，以年、月、日衡量</h4><p><img src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/Java%E5%B8%B8%E7%94%A8%E7%B1%BB/%E6%97%A5%E6%9C%9F%E9%97%B4%E9%9A%94.png" alt="image-20200530101601375"></p>
<p>举例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test4</span><span class="params">()</span></span>&#123;</span><br><span class="line">	LocalDate localDate = LocalDate.now();</span><br><span class="line">	LocalDate localDate1 = LocalDate.of(<span class="number">2028</span>, <span class="number">3</span>, <span class="number">18</span>);</span><br><span class="line">	</span><br><span class="line">	Period period = Period.between(localDate, localDate1);</span><br><span class="line">	System.out.println(period);</span><br><span class="line">	</span><br><span class="line">	System.out.println(period.getYears());</span><br><span class="line">	System.out.println(period.getMonths());</span><br><span class="line">	System.out.println(period.getDays());</span><br><span class="line">	</span><br><span class="line">	Period period1 = period.withYears(<span class="number">2</span>);</span><br><span class="line">	System.out.println(period1);</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="7-4-日期时间校正器：TemporalAdjuster"><a href="#7-4-日期时间校正器：TemporalAdjuster" class="headerlink" title="7.4 日期时间校正器：TemporalAdjuster"></a>7.4 日期时间校正器：TemporalAdjuster</h4><p>举例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test5</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="comment">//获取当前日期的下一个周日是哪天？</span></span><br><span class="line">		TemporalAdjuster temporalAdjuster = TemporalAdjusters.next(DayOfWeek.SUNDAY);</span><br><span class="line">		</span><br><span class="line">		LocalDateTime localDateTime = LocalDateTime.now().with(temporalAdjuster);</span><br><span class="line">		System.out.println(localDateTime);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//获取下一个工作日是哪天？</span></span><br><span class="line">		LocalDate localDate = LocalDate.now().with(<span class="keyword">new</span> TemporalAdjuster()&#123;</span><br><span class="line"></span><br><span class="line">			<span class="meta">@Override</span></span><br><span class="line">			<span class="function"><span class="keyword">public</span> Temporal <span class="title">adjustInto</span><span class="params">(Temporal temporal)</span> </span>&#123;</span><br><span class="line">				LocalDate date = (LocalDate)temporal;</span><br><span class="line">				<span class="keyword">if</span>(date.getDayOfWeek().equals(DayOfWeek.FRIDAY))&#123;</span><br><span class="line">					<span class="keyword">return</span> date.plusDays(<span class="number">3</span>);</span><br><span class="line">				&#125;<span class="keyword">else</span> <span class="keyword">if</span>(date.getDayOfWeek().equals(DayOfWeek.SATURDAY))&#123;</span><br><span class="line">					<span class="keyword">return</span> date.plusDays(<span class="number">2</span>);</span><br><span class="line">				&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">					<span class="keyword">return</span> date.plusDays(<span class="number">1</span>);</span><br><span class="line">				&#125;</span><br><span class="line">					</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">		&#125;);</span><br><span class="line">		</span><br><span class="line">		System.out.println(<span class="string">&quot;下一个工作日是：&quot;</span> + localDate);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="五、Java比较器"><a href="#五、Java比较器" class="headerlink" title="五、Java比较器"></a>五、Java比较器</h2><h3 id="1-Java比较器的使用背景："><a href="#1-Java比较器的使用背景：" class="headerlink" title="1.Java比较器的使用背景："></a>1.Java比较器的使用背景：</h3><p>Java中的对象，正常情况下，只能进行比较：==  或  != 。不能使用 &gt; 或 &lt; 的<br>但是在开发场景中，我们需要对多个对象进行排序，言外之意，就需要比较对象的大小。<br>如何实现？使用两个接口中的任何一个：Comparable 或 Comparator</p>
<h3 id="2-自然排序：使用Comparable接口"><a href="#2-自然排序：使用Comparable接口" class="headerlink" title="2.自然排序：使用Comparable接口"></a>2.自然排序：使用Comparable接口</h3><p>2.1 说明</p>
<ol>
<li><p>像String、包装类等实现了Comparable接口，重写了compareTo(obj)方法，给出了比较两个对象大小的方式。</p>
</li>
<li><p>像String、包装类重写compareTo()方法以后，进行了从小到大的排列</p>
</li>
<li><p>重写compareTo(obj)的规则：<br> 如果当前对象this大于形参对象obj，则返回正整数，<br> 如果当前对象this小于形参对象obj，则返回负整数，<br> 如果当前对象this等于形参对象obj，则返回零。</p>
</li>
<li><p>对于自定义类来说，如果需要排序，我们可以让自定义类实现Comparable接口，重写compareTo(obj)方法。在compareTo(obj)方法中指明如何排序</p>
</li>
</ol>
<p>2.2 自定义类代码举例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Goods</span> <span class="keyword">implements</span>  <span class="title">Comparable</span></span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">double</span> price;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//指明商品比较大小的方式:照价格从低到高排序,再照产品名称从高到低排序</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">compareTo</span><span class="params">(Object o)</span> </span>&#123;</span><br><span class="line"><span class="comment">//        System.out.println(&quot;**************&quot;);</span></span><br><span class="line">        <span class="keyword">if</span>(o <span class="keyword">instanceof</span> Goods)&#123;</span><br><span class="line">            Goods goods = (Goods)o;</span><br><span class="line">            <span class="comment">//方式一：</span></span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.price &gt; goods.price)&#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">            &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="keyword">this</span>.price &lt; goods.price)&#123;</span><br><span class="line">                <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line"><span class="comment">//                return 0;</span></span><br><span class="line">               <span class="keyword">return</span> -<span class="keyword">this</span>.name.compareTo(goods.name);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//方式二：</span></span><br><span class="line"><span class="comment">//           return Double.compare(this.price,goods.price);</span></span><br><span class="line">        &#125;</span><br><span class="line"><span class="comment">//        return 0;</span></span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">&quot;传入的数据类型不一致！&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"><span class="comment">// getter、setter、toString()、构造器：省略</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-定制排序：使用Comparator接口"><a href="#3-定制排序：使用Comparator接口" class="headerlink" title="3.定制排序：使用Comparator接口"></a>3.定制排序：使用Comparator接口</h3><p>3.1 说明<br>1.背景：<br>当元素的类型没实现java.lang.Comparable接口而又不方便修改代码，或者实现了java.lang.Comparable接口的排序规则不适合当前的操作，那么可以考虑使用 Comparator 的对象来排序<br>2.重写compare(Object o1,Object o2)方法，比较o1和o2的大小：<br>如果方法返回正整数，则表示o1大于o2；<br>如果返回0，表示相等；<br>返回负整数，表示o1小于o2。</p>
<p>3.2 代码举例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">Comparator com = <span class="keyword">new</span> Comparator() &#123;</span><br><span class="line">    <span class="comment">//指明商品比较大小的方式:照产品名称从低到高排序,再照价格从高到低排序</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">compare</span><span class="params">(Object o1, Object o2)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(o1 <span class="keyword">instanceof</span> Goods &amp;&amp; o2 <span class="keyword">instanceof</span> Goods)&#123;</span><br><span class="line">            Goods g1 = (Goods)o1;</span><br><span class="line">            Goods g2 = (Goods)o2;</span><br><span class="line">            <span class="keyword">if</span>(g1.getName().equals(g2.getName()))&#123;</span><br><span class="line">                <span class="keyword">return</span> -Double.compare(g1.getPrice(),g2.getPrice());</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                <span class="keyword">return</span> g1.getName().compareTo(g2.getName());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">&quot;输入的数据类型不一致&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>使用：<br>Arrays.sort(goods,com);<br>Collections.sort(coll,com);<br>new TreeSet(com);</p>
<h3 id="4-两种排序方式对比"><a href="#4-两种排序方式对比" class="headerlink" title="4.两种排序方式对比"></a>4.两种排序方式对比</h3><ul>
<li>   Comparable接口的方式一旦一定，保证Comparable接口实现类的对象在任何位置都可以比较大小。</li>
<li>   Comparator接口属于临时性的比较。</li>
</ul>
<h2 id="六、其他类"><a href="#六、其他类" class="headerlink" title="六、其他类"></a>六、其他类</h2><h3 id="1-System类"><a href="#1-System类" class="headerlink" title="1.System类"></a>1.System类</h3><p>System类代表系统，系统级的很多属性和控制方法都放置在该类的内部。该类位于java.lang包。<br>由于该类的构造器是private的，所以无法创建该类的对象，也就是无法实例化该类。其内部的成员变量和成员方法都是static的，所以也可以很方便的进行调用。<br>方法：<br>native long currentTimeMillis()<br>void exit(int status)<br>void gc()<br>String getProperty(String key)</p>
<h3 id="2-Math类"><a href="#2-Math类" class="headerlink" title="2.Math类"></a>2.Math类</h3><p>java.lang.Math提供了一系列静态方法用于科学计算。其方法的参数和返回值类型一般为double型。</p>
<h3 id="3-BigInteger类、BigDecimal类"><a href="#3-BigInteger类、BigDecimal类" class="headerlink" title="3.BigInteger类、BigDecimal类"></a>3.BigInteger类、BigDecimal类</h3><p>说明：<br>① java.math包的BigInteger可以表示不可变的任意精度的整数。<br>② 要求数字精度比较高，用到java.math.BigDecimal类</p>
<p>代码举例：</p>
<p><img src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/Java%E5%B8%B8%E7%94%A8%E7%B1%BB/BigInterger%E7%B1%BB%E5%92%8CBigDecimal.png" alt="image-20200530102021703"></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Kuikui Hu</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://kuikui1998.gitee.io/2020/06/01/Java%E5%B8%B8%E7%94%A8%E7%B1%BB/">https://kuikui1998.gitee.io/2020/06/01/Java%E5%B8%B8%E7%94%A8%E7%B1%BB/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://kuikui1998.gitee.io" target="_blank">kuikui</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Java/">Java</a><a class="post-meta__tags" href="/tags/Java%E9%AB%98%E7%BA%A7/">Java高级</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/java.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/06/02/Java%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%92%8C%E6%B3%A8%E8%A7%A3/"><img class="prev-cover" src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/java.jpg" onerror="onerror=null;src='/img/404.jpg'"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Java高级学习之-枚举类和注解</div></div></a></div><div class="next-post pull-right"><a href="/2020/05/31/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B/"><img class="next-cover" src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/java.jpg" onerror="onerror=null;src='/img/404.jpg'"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Java高级学习之-多线程</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2020/06/02/Java枚举类和注解/" title="Java高级学习之-枚举类和注解"><img class="cover" src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/java.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-06-02</div><div class="title">Java高级学习之-枚举类和注解</div></div></a></div><div><a href="/2020/06/06/Java泛型/" title="Java高级学习之-泛型"><img class="cover" src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/java.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-06-06</div><div class="title">Java高级学习之-泛型</div></div></a></div><div><a href="/2020/06/16/Java网络编程/" title="Java高级学习之-网络编程"><img class="cover" src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/java.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-06-16</div><div class="title">Java高级学习之-网络编程</div></div></a></div><div><a href="/2020/06/11/JavaIO流/" title="Java高级学习之-IO流"><img class="cover" src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/java.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-06-11</div><div class="title">Java高级学习之-IO流</div></div></a></div><div><a href="/2020/06/18/Java反射机制/" title="Java高级学习之-反射"><img class="cover" src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/java.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-06-18</div><div class="title">Java高级学习之-反射</div></div></a></div><div><a href="/2020/05/31/Java多线程/" title="Java高级学习之-多线程"><img class="cover" src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JavaSE/java.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-05-31</div><div class="title">Java高级学习之-多线程</div></div></a></div></div></div></div><div class="aside_content" id="aside_content"><div class="card-widget card-info"><div class="card-content"><div class="card-info-avatar is-center"><img class="avatar-img" src="/img/me.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">Kuikui Hu</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">11</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/kuikui1998"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/kuikui1998" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="/shakuil@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div></div><div class="card-widget card-announcement"><div class="card-content"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">Welcome Kuikui's Blog</div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="card-content"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Java%E5%B8%B8%E7%94%A8%E7%B1%BB"><span class="toc-number">1.</span> <span class="toc-text">Java常用类</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81String%E7%B1%BB"><span class="toc-number">1.1.</span> <span class="toc-text">一、String类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="toc-number">1.1.1.</span> <span class="toc-text">1.概述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-String%E7%9A%84%E4%B8%8D%E5%8F%AF%E5%8F%98%E6%80%A7"><span class="toc-number">1.1.2.</span> <span class="toc-text">2.String的不可变性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-String%E5%AE%9E%E4%BE%8B%E5%8C%96%E7%9A%84%E4%B8%8D%E5%90%8C%E6%96%B9%E5%BC%8F"><span class="toc-number">1.1.3.</span> <span class="toc-text">3.String实例化的不同方式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%8B%BC%E6%8E%A5%E6%96%B9%E5%BC%8F%E8%B5%8B%E5%80%BC%E7%9A%84%E5%AF%B9%E6%AF%94"><span class="toc-number">1.1.4.</span> <span class="toc-text">4.字符串拼接方式赋值的对比</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-String%E4%B8%8E%E5%85%B6%E5%AE%83%E7%BB%93%E6%9E%84%E7%9A%84%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.1.5.</span> <span class="toc-text">6.String与其它结构的转换</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#6-1-%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E3%80%81%E5%8C%85%E8%A3%85%E7%B1%BB%E4%B9%8B%E9%97%B4%E7%9A%84%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.1.5.1.</span> <span class="toc-text">6.1 与基本数据类型、包装类之间的转换</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-2-%E4%B8%8E%E5%AD%97%E7%AC%A6%E6%95%B0%E7%BB%84%E4%B9%8B%E9%97%B4%E7%9A%84%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.1.5.2.</span> <span class="toc-text">6.2 与字符数组之间的转换</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-3-%E4%B8%8E%E5%AD%97%E8%8A%82%E6%95%B0%E7%BB%84%E4%B9%8B%E9%97%B4%E7%9A%84%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.1.5.3.</span> <span class="toc-text">6.3 与字节数组之间的转换</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-4-%E4%B8%8EStringBuffer%E3%80%81StringBuilder%E4%B9%8B%E9%97%B4%E7%9A%84%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.1.5.4.</span> <span class="toc-text">6.4 与StringBuffer、StringBuilder之间的转换</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-JVM%E4%B8%AD%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%B8%B8%E9%87%8F%E6%B1%A0%E5%AD%98%E6%94%BE%E4%BD%8D%E7%BD%AE%E8%AF%B4%E6%98%8E%EF%BC%9A"><span class="toc-number">1.1.6.</span> <span class="toc-text">7.JVM中字符串常量池存放位置说明：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-%E5%B8%B8%E8%A7%81%E7%AE%97%E6%B3%95%E9%A2%98%E7%9B%AE%E7%9A%84%E8%80%83%E6%9F%A5%EF%BC%9A"><span class="toc-number">1.1.7.</span> <span class="toc-text">8.常见算法题目的考查：</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81StringBuffer%E3%80%81StringBuilder"><span class="toc-number">1.2.</span> <span class="toc-text">二、StringBuffer、StringBuilder</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-String%E3%80%81StringBuffer%E3%80%81StringBuilder%E4%B8%89%E8%80%85%E7%9A%84%E5%AF%B9%E6%AF%94"><span class="toc-number">1.2.1.</span> <span class="toc-text">1.String、StringBuffer、StringBuilder三者的对比</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-StringBuffer%E4%B8%8EStringBuilder%E7%9A%84%E5%86%85%E5%AD%98%E8%A7%A3%E6%9E%90"><span class="toc-number">1.2.2.</span> <span class="toc-text">2.StringBuffer与StringBuilder的内存解析</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E5%AF%B9%E6%AF%94String%E3%80%81StringBuffer%E3%80%81StringBuilder%E4%B8%89%E8%80%85%E7%9A%84%E6%89%A7%E8%A1%8C%E6%95%88%E7%8E%87"><span class="toc-number">1.2.3.</span> <span class="toc-text">3.对比String、StringBuffer、StringBuilder三者的执行效率</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-StringBuffer%E3%80%81StringBuilder%E4%B8%AD%E7%9A%84%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-number">1.2.4.</span> <span class="toc-text">4.StringBuffer、StringBuilder中的常用方法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81JDK-8%E4%B9%8B%E5%89%8D%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4API"><span class="toc-number">1.3.</span> <span class="toc-text">三、JDK 8之前日期时间API</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E8%8E%B7%E5%8F%96%E7%B3%BB%E7%BB%9F%E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4%EF%BC%9ASystem%E7%B1%BB%E4%B8%AD%E7%9A%84currentTimeMillis"><span class="toc-number">1.3.1.</span> <span class="toc-text">1.获取系统当前时间：System类中的currentTimeMillis()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-java-util-Date%E7%B1%BB%E4%B8%8Ejava-sql-Date%E7%B1%BB"><span class="toc-number">1.3.2.</span> <span class="toc-text">2.java.util.Date类与java.sql.Date类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-java-text-SimpleDataFormat%E7%B1%BB"><span class="toc-number">1.3.3.</span> <span class="toc-text">3.java.text.SimpleDataFormat类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-Calendar%E7%B1%BB%EF%BC%9A%E6%97%A5%E5%8E%86%E7%B1%BB%E3%80%81%E6%8A%BD%E8%B1%A1%E7%B1%BB"><span class="toc-number">1.3.4.</span> <span class="toc-text">4.Calendar类：日历类、抽象类</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81JDK8%E4%B8%AD%E6%96%B0%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4API"><span class="toc-number">1.4.</span> <span class="toc-text">四、JDK8中新日期时间API</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4API%E7%9A%84%E8%BF%AD%E4%BB%A3%EF%BC%9A"><span class="toc-number">1.4.1.</span> <span class="toc-text">1.日期时间API的迭代：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%89%8D%E4%B8%A4%E4%BB%A3%E5%AD%98%E5%9C%A8%E7%9A%84%E9%97%AE%E9%A2%98%E4%B8%BE%E4%BE%8B%EF%BC%9A"><span class="toc-number">1.4.2.</span> <span class="toc-text">2.前两代存在的问题举例：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-java-8-%E4%B8%AD%E6%96%B0%E7%9A%84%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4API%E6%B6%89%E5%8F%8A%E5%88%B0%E7%9A%84%E5%8C%85"><span class="toc-number">1.4.3.</span> <span class="toc-text">3.java 8 中新的日期时间API涉及到的包</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E6%9C%AC%E5%9C%B0%E6%97%A5%E6%9C%9F%E3%80%81%E6%9C%AC%E5%9C%B0%E6%97%B6%E9%97%B4%E3%80%81%E6%9C%AC%E5%9C%B0%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4%E7%9A%84%E4%BD%BF%E7%94%A8%EF%BC%9ALocalDate-LocalTime-LocalDateTime"><span class="toc-number">1.4.4.</span> <span class="toc-text">4.&#x3D;&#x3D;本地日期、本地时间、本地日期时间的使用：LocalDate &#x2F; LocalTime &#x2F; LocalDateTime&#x3D;&#x3D;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E6%97%B6%E9%97%B4%E7%82%B9%EF%BC%9AInstant"><span class="toc-number">1.4.5.</span> <span class="toc-text">&#x3D;&#x3D;5.时间点：Instant&#x3D;&#x3D;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4%E6%A0%BC%E5%BC%8F%E5%8C%96%E7%B1%BB%EF%BC%9ADateTimeFormatter"><span class="toc-number">1.4.6.</span> <span class="toc-text">6.日期时间格式化类：DateTimeFormatter</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-%E5%85%B6%E5%AE%83API%E7%9A%84%E4%BD%BF%E7%94%A8-%EF%BC%88%E4%B8%8D%E8%AE%B2%EF%BC%89"><span class="toc-number">1.4.7.</span> <span class="toc-text">7.其它API的使用 （不讲）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#7-1-%E5%B8%A6%E6%97%B6%E5%8C%BA%E7%9A%84%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4%EF%BC%9AZonedDateTime-ZoneId"><span class="toc-number">1.4.7.1.</span> <span class="toc-text">7.1 带时区的日期时间：ZonedDateTime &#x2F; ZoneId</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-2-%E6%97%B6%E9%97%B4%E9%97%B4%E9%9A%94%EF%BC%9ADuration%E2%80%93%E7%94%A8%E4%BA%8E%E8%AE%A1%E7%AE%97%E4%B8%A4%E4%B8%AA%E2%80%9C%E6%97%B6%E9%97%B4%E2%80%9D%E9%97%B4%E9%9A%94%EF%BC%8C%E4%BB%A5%E7%A7%92%E5%92%8C%E7%BA%B3%E7%A7%92%E4%B8%BA%E5%9F%BA%E5%87%86"><span class="toc-number">1.4.7.2.</span> <span class="toc-text">7.2 时间间隔：Duration–用于计算两个“时间”间隔，以秒和纳秒为基准</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-3-%E6%97%A5%E6%9C%9F%E9%97%B4%E9%9A%94%EF%BC%9APeriod-%E2%80%93%E7%94%A8%E4%BA%8E%E8%AE%A1%E7%AE%97%E4%B8%A4%E4%B8%AA%E2%80%9C%E6%97%A5%E6%9C%9F%E2%80%9D%E9%97%B4%E9%9A%94%EF%BC%8C%E4%BB%A5%E5%B9%B4%E3%80%81%E6%9C%88%E3%80%81%E6%97%A5%E8%A1%A1%E9%87%8F"><span class="toc-number">1.4.7.3.</span> <span class="toc-text">7.3 日期间隔：Period –用于计算两个“日期”间隔，以年、月、日衡量</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-4-%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4%E6%A0%A1%E6%AD%A3%E5%99%A8%EF%BC%9ATemporalAdjuster"><span class="toc-number">1.4.7.4.</span> <span class="toc-text">7.4 日期时间校正器：TemporalAdjuster</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81Java%E6%AF%94%E8%BE%83%E5%99%A8"><span class="toc-number">1.5.</span> <span class="toc-text">五、Java比较器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-Java%E6%AF%94%E8%BE%83%E5%99%A8%E7%9A%84%E4%BD%BF%E7%94%A8%E8%83%8C%E6%99%AF%EF%BC%9A"><span class="toc-number">1.5.1.</span> <span class="toc-text">1.Java比较器的使用背景：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E8%87%AA%E7%84%B6%E6%8E%92%E5%BA%8F%EF%BC%9A%E4%BD%BF%E7%94%A8Comparable%E6%8E%A5%E5%8F%A3"><span class="toc-number">1.5.2.</span> <span class="toc-text">2.自然排序：使用Comparable接口</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E5%AE%9A%E5%88%B6%E6%8E%92%E5%BA%8F%EF%BC%9A%E4%BD%BF%E7%94%A8Comparator%E6%8E%A5%E5%8F%A3"><span class="toc-number">1.5.3.</span> <span class="toc-text">3.定制排序：使用Comparator接口</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E4%B8%A4%E7%A7%8D%E6%8E%92%E5%BA%8F%E6%96%B9%E5%BC%8F%E5%AF%B9%E6%AF%94"><span class="toc-number">1.5.4.</span> <span class="toc-text">4.两种排序方式对比</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E5%85%B6%E4%BB%96%E7%B1%BB"><span class="toc-number">1.6.</span> <span class="toc-text">六、其他类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-System%E7%B1%BB"><span class="toc-number">1.6.1.</span> <span class="toc-text">1.System类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-Math%E7%B1%BB"><span class="toc-number">1.6.2.</span> <span class="toc-text">2.Math类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-BigInteger%E7%B1%BB%E3%80%81BigDecimal%E7%B1%BB"><span class="toc-number">1.6.3.</span> <span class="toc-text">3.BigInteger类、BigDecimal类</span></a></li></ol></li></ol></li></ol></div></div></div><div class="card-widget card-recent-post"><div class="card-content"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/01/05/JVM%E7%B3%BB%E5%88%97-%E7%AC%AC4%E7%AB%A0-%E8%99%9A%E6%8B%9F%E6%9C%BA%E6%A0%88/" title="JVM系列-第4章-虚拟机栈"><img src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JVM/jvm.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JVM系列-第4章-虚拟机栈"/></a><div class="content"><a class="title" href="/2021/01/05/JVM%E7%B3%BB%E5%88%97-%E7%AC%AC4%E7%AB%A0-%E8%99%9A%E6%8B%9F%E6%9C%BA%E6%A0%88/" title="JVM系列-第4章-虚拟机栈">JVM系列-第4章-虚拟机栈</a><time datetime="2021-01-05T07:31:58.000Z" title="发表于 2021-01-05 15:31:58">2021-01-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2020/12/11/JVM%E7%B3%BB%E5%88%97-%E7%AC%AC3%E7%AB%A0-%E8%BF%90%E8%A1%8C%E6%97%B6%E6%95%B0%E6%8D%AE%E5%8C%BA/" title="JVM系列-第3章-运行时数据区"><img src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JVM/jvm.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JVM系列-第3章-运行时数据区"/></a><div class="content"><a class="title" href="/2020/12/11/JVM%E7%B3%BB%E5%88%97-%E7%AC%AC3%E7%AB%A0-%E8%BF%90%E8%A1%8C%E6%97%B6%E6%95%B0%E6%8D%AE%E5%8C%BA/" title="JVM系列-第3章-运行时数据区">JVM系列-第3章-运行时数据区</a><time datetime="2020-12-11T07:31:58.000Z" title="发表于 2020-12-11 15:31:58">2020-12-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2020/12/11/JVM%E7%B3%BB%E5%88%97-%E7%AC%AC2%E7%AB%A0-%E7%B1%BB%E5%8A%A0%E8%BD%BD%E5%AD%90%E7%B3%BB%E7%BB%9F/" title="JVM系列-第2章-类加载子系统"><img src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JVM/jvm.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JVM系列-第2章-类加载子系统"/></a><div class="content"><a class="title" href="/2020/12/11/JVM%E7%B3%BB%E5%88%97-%E7%AC%AC2%E7%AB%A0-%E7%B1%BB%E5%8A%A0%E8%BD%BD%E5%AD%90%E7%B3%BB%E7%BB%9F/" title="JVM系列-第2章-类加载子系统">JVM系列-第2章-类加载子系统</a><time datetime="2020-12-11T06:31:58.000Z" title="发表于 2020-12-11 14:31:58">2020-12-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2020/12/11/JVM%E7%B3%BB%E5%88%97-%E7%AC%AC1%E7%AB%A0-JVM%E4%B8%8EJava%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84/" title="JVM系列-第1章-JVM与Java体系结构"><img src="https://gitee.com/kuikui1998/images_ben/raw/master/images/java/JVM/jvm.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JVM系列-第1章-JVM与Java体系结构"/></a><div class="content"><a class="title" href="/2020/12/11/JVM%E7%B3%BB%E5%88%97-%E7%AC%AC1%E7%AB%A0-JVM%E4%B8%8EJava%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84/" title="JVM系列-第1章-JVM与Java体系结构">JVM系列-第1章-JVM与Java体系结构</a><time datetime="2020-12-11T00:51:56.000Z" title="发表于 2020-12-11 08:51:56">2020-12-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2020/12/09/docker%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" title="docker常用命令"><img src="https://gitee.com/kuikui1998/images_ben/raw/master/images/tools/docker.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="docker常用命令"/></a><div class="content"><a class="title" href="/2020/12/09/docker%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" title="docker常用命令">docker常用命令</a><time datetime="2020-12-09T09:25:56.000Z" title="发表于 2020-12-09 17:25:56">2020-12-09</time></div></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By Kuikui Hu</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> {preloader.endLoading()})</script><div class="js-pjax"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="true"></script></div></body></html>